package com.thingple.usb.session;

import java.util.HashMap;
import java.util.Map;

public class SerialPortConfig {
    
    public static final byte BaudRate300 = 0x00;
    public static final byte BaudRate600 = 0x01;
    public static final byte BaudRate1200 = 0x02;
    public static final byte BaudRate2400 = 0x03;
    public static final byte BaudRate4800 = 0x04;
    public static final byte BaudRate9600 = 0x05;
    public static final byte BaudRate19200 = 0x06;
    public static final byte BaudRate38400 = 0x07;
    public static final byte BaudRate57600 = 0x08;
    public static final byte BaudRate115200 = 0x09;
    public static final byte BaudRate230400 = 0x0A;
    public static final byte BaudRate460800 = 0x0B;
    public static final byte BaudRate921600 = 0x0C;
    public static byte BaudRateDefault = BaudRate9600; // 可编辑
    
    public static final Map<Integer, Byte> BaudRateMap = new HashMap<>();
    
    static {
        BaudRateMap.put(300, BaudRate300);
        BaudRateMap.put(600, BaudRate600);
        BaudRateMap.put(1200, BaudRate1200);
        BaudRateMap.put(2400, BaudRate2400);
        BaudRateMap.put(4800, BaudRate4800);
        BaudRateMap.put(9600, BaudRate9600);
        BaudRateMap.put(19200, BaudRate19200);
        BaudRateMap.put(38400, BaudRate38400);
        BaudRateMap.put(57600, BaudRate57600);
        BaudRateMap.put(115200, BaudRate115200);
        BaudRateMap.put(230400, BaudRate230400);
        BaudRateMap.put(460800, BaudRate460800);
        BaudRateMap.put(921600, BaudRate921600);
    }

    /**
     * 返回baudRate枚举, 枚举不到使用默认值
     * @param baudRate int类型波特率
     * @return 枚举
     */
    public static byte getBaudRate(int baudRate) {
        Byte br = BaudRateMap.get(baudRate);
        if (br == null) {
            br = BaudRateDefault;
        }
        return br;
    }

    public static final byte DataBits5 = 0x00;
    public static final byte DataBits6 = 0x01;
    public static final byte DataBits7 = 0x02;
    public static final byte DataBits8 = 0x03;
    public static byte DataBitsDefault = DataBits8;

    public static final Map<Integer, Byte> DataBitsMap = new HashMap<>();
    static {
        DataBitsMap.put(5, DataBits5);
        DataBitsMap.put(6, DataBits6);
        DataBitsMap.put(7, DataBits7);
        DataBitsMap.put(8, DataBits8);
    }

    /**
     * 返回dataBits枚举, 枚举不到使用默认值
     * @param dataBits int类型
     * @return 枚举
     */
    public static byte getDataBits(int dataBits) {
        Byte db = DataBitsMap.get(dataBits);
        if (db == null) {
            db = DataBitsDefault;
        }
        return db;
    }
}
